package com.woniuxy.propertyjava.controller;


import com.woniuxy.propertyjava.entity.Car;
import com.woniuxy.propertyjava.entity.PageBean;
import com.woniuxy.propertyjava.entity.ResponseResult;
import com.woniuxy.propertyjava.service.CarService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Car)表控制层
 *
 * @author makejava
 * @since 2022-07-06 09:59:30
 */
@RestController
@RequestMapping
public class CarController {
    /**
     * 服务对象
     */
    @Resource
    private CarService carService;

    /**
     * 分页查询
     *
     * @param car         筛选条件
     * @param pageIndex 分页对象
     * @return 查询结果
     */
    @GetMapping("/car/{pageIndex}/{pageSize}")
    public ResponseResult<PageBean<Car>> queryByPage(@RequestBody Car car,
                                                     @PathVariable int pageIndex,
                                                     @PathVariable int pageSize) {
        PageBean<Car> pageBean = carService.queryByPage(car, pageIndex, pageSize);

        return new ResponseResult<>(200,"ok",pageBean);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/car/{id}")
    public ResponseResult<Car> queryById(@PathVariable("id") Integer id) {

        return new ResponseResult<>(200,"ok",carService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param car 实体
     * @return 新增结果
     */
    @PostMapping("/car")
    public ResponseResult<Car> add(@RequestBody Car car) {


        return new ResponseResult<>(200,"ok",carService.queryById(carService.insert(car).getId()));
    }

    /**
     * 编辑数据
     *
     * @param car 实体
     * @return 编辑结果
     */
    @PutMapping("/car")
    public ResponseResult<Car> edit(@RequestBody Car car) {


        return new ResponseResult<>(200,"ok",carService.queryById(carService.update(car).getId()));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/car")
    public ResponseResult deleteById(Integer id) {
        carService.deleteById(id);
        return new ResponseResult(200,"ok","");
    }

}

